package cn.Y_Qiu.pet.controller;

import cn.Y_Qiu.basic.util.Ajaxresult;
import cn.Y_Qiu.basic.util.PageList;
import cn.Y_Qiu.pet.domain.Pet;
import cn.Y_Qiu.pet.query.PetQuery;
import cn.Y_Qiu.pet.service.IPetService;
import cn.Y_Qiu.pet.util.LoginContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @PostMapping
    public PageList<Pet> getPet(@RequestBody PetQuery query, HttpServletRequest request){
        return petService.queryPage(query);
    }

    @PostMapping("/onsale")
    public Ajaxresult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me()
                    .setMessage("操作失败！");
        }
    }
    @PostMapping("/offsale")
    public Ajaxresult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me()
                    .setMessage("操作失败！");
        }
    }
    @GetMapping("/detail/{id}")
    public Pet getDetailById(@PathVariable("id") Long id){
        Pet pet = petService.queryById(id);
        return pet;
    }
    @GetMapping("/{id}")
    public Pet getOrderDetailById(@PathVariable("id") Long id){
        Pet pet = petService.queryById(id);
        return pet;
    }
    @GetMapping("/adopt/{id}")
    public Ajaxresult adopt(@PathVariable("id") Long id,HttpServletRequest request){
        Long buyerId = LoginContext.getLoginInfo(request).getId();
        try {
            petService.adpot(id,buyerId);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("领养出错");
        }
    }
}
